package com.iwomy.secureplat.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicAlarmContactsTimeDTO;
import com.iwomy.secureplat.platform.entity.BasicAlarmContactsEntity;
import com.iwomy.secureplat.platform.entity.BasicAlarmContactsTimeEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.mapper.BasicAlarmContactsTimeMapper;
import com.iwomy.secureplat.platform.service.BasicAlarmContactsService;
import com.iwomy.secureplat.platform.service.BasicAlarmContactsTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 接警人接警时段表
 *
 * @author pig
 * @date 2024-01-03 15:41:31
 */
@Service
public class BasicAlarmContactsTimeServiceImpl extends ServiceImpl<BasicAlarmContactsTimeMapper, BasicAlarmContactsTimeEntity> implements BasicAlarmContactsTimeService {

	@Autowired
	private BasicAlarmContactsService alarmContactsService;

	@Override
	public R newlyAdded(BasicAlarmContactsTimeDTO basicAlarmContactsTimeDTO) {
		List<Map<String, String>> fromData = basicAlarmContactsTimeDTO.getFormData();
		List<BasicAlarmContactsTimeEntity> alarmContactsTimeList = new ArrayList<>();
		for (Map<String, String> form : fromData) {
			String startTimeStr = form.get("startTime");
			String endTimeStr = form.get("endTime");
			LocalTime startTime = LocalTime.parse(startTimeStr);
			LocalTime endTime = LocalTime.parse(endTimeStr);

			boolean isOverlap = false;
			for (BasicAlarmContactsTimeEntity entity : alarmContactsTimeList) {
				LocalTime existingStartTime = LocalTime.parse(entity.getStartTime());
				LocalTime existingEndTime = LocalTime.parse(entity.getEndTime());
				if (isTimeOverlap(startTime, endTime, existingStartTime, existingEndTime)) {
					isOverlap = true;
					break;
				}
			}
			if (isOverlap) {
				throw new PlatformException("时间段存在交叉，请检查后再保存");
			}
			BasicAlarmContactsTimeEntity alarmContactsTimeEntity = new BasicAlarmContactsTimeEntity();
			alarmContactsTimeEntity.setStartTime(startTimeStr);
			alarmContactsTimeEntity.setEndTime(endTimeStr);
			alarmContactsTimeEntity.setAlarmContactsId(basicAlarmContactsTimeDTO.getAlarmContactsId());
			alarmContactsTimeEntity.setDeptId(basicAlarmContactsTimeDTO.getDeptId());
			alarmContactsTimeEntity.setTenantId(SecurityUtils.getUser().getTenantId());
			alarmContactsTimeList.add(alarmContactsTimeEntity);
		}
		//保存之前先删除改接警人的时间段
		this.remove(Wrappers.<BasicAlarmContactsTimeEntity>lambdaQuery()
				.eq(BasicAlarmContactsTimeEntity::getAlarmContactsId, basicAlarmContactsTimeDTO.getAlarmContactsId()));
		// 在循环结束后进行保存操作
		this.saveBatch(alarmContactsTimeList);
		BasicAlarmContactsEntity wrapperEntity = new BasicAlarmContactsEntity();
		wrapperEntity.setId(basicAlarmContactsTimeDTO.getAlarmContactsId());
		LambdaQueryWrapper<BasicAlarmContactsEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicAlarmContactsEntity basicAlarmContacts = alarmContactsService.getOne(wrapper);
		basicAlarmContacts.setAlarmTime(basicAlarmContactsTimeDTO.getAlarmTime());
		alarmContactsService.updateById(basicAlarmContacts);
		return R.ok();
	}

	//比较时间段是否交叉
	public boolean isTimeOverlap(LocalTime startTime1, LocalTime endTime1,
								 LocalTime startTime2, LocalTime endTime2) {
		if (startTime1.isBefore(endTime1)) {
			return (startTime1.isBefore(endTime2) || startTime1.equals(endTime2)) &&
					(endTime1.isAfter(startTime2) || endTime1.equals(startTime2));
		} else {
			return (startTime1.isBefore(endTime2) || startTime1.equals(endTime2)) ||
					(endTime1.isAfter(startTime2) || endTime1.equals(startTime2));
		}
	}
}